library(ggplot2)
library(data.table)
library(scales)
library(tikzDevice)

setwd("/Users/jbhoward/Documents/Personal Research/JAERE Extension/Draft/Code/appendix_code")
dt <- fread("appendix_model.csv")

dt[, subnat := as.character(subnat)]
dt[, country := as.character(country)]
dt[subnat == 1, subnat := "Periphery"]
dt[subnat == 2, subnat := "Core"]
dt[country == 1, country := "Home"]
dt[country == 2, country := "Foreign"]

reg <- dt[, list(emissions = sum(emissions * g_chi),
          ttl_output = sum(ttl_output * g_chi),
          wages = unique(wages),
          P_H = unique(P_H),
          labor = sum(labor * g_chi),
          mass_cus = sum(mass_cus * g_chi),
          mass_sup = sum(mass_sup * g_chi),
          FA = sum(FA * g_chi),
          kappa = sum(kappa * g_chi),
          dirty_input = sum(dirty_input * g_chi),
          int_purch_val = sum(int_purch_val * g_chi)),
    by = list(subnat, country, inter_trade_cost)]


reg <- reg[country == "Home"]
reg[, intensity := emissions/ttl_output]
reg[, ttl_expenditures := wages * labor + FA + dirty_input * kappa + int_purch_val]
reg[, int_share := int_purch_val/ttl_expenditures]
reg[, network_degree := mass_cus + mass_sup]
reg[, abate_share := FA/ttl_expenditures]

max_tau <- sort(unique(reg[, inter_trade_cost]), decreasing = T)[3] #max(reg[, inter_trade_cost])
min_tau <- min(reg[, inter_trade_cost])
reg <- reg[inter_trade_cost %in% c(min_tau, max_tau)]

normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, emissions]
reg[, emissions_norm := emissions/normalizer]

normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, FA]
reg[, abate_norm := FA/normalizer]

normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, intensity]
reg[, intensity_norm := intensity/normalizer]

envr <- reg[, list(inter_trade_cost, subnat, emissions_norm, intensity_norm, abate_norm)]
envr <- melt(envr, 1:2)

envr[variable == "emissions_norm", variable := "(a) Emissions"]
envr[variable == "intensity_norm", variable := "(b) Emissions Intensity"]
envr[variable == "abate_norm", variable := "(c) Abatement"]


tikz(file = "appendix_environment.tex", width = 12, height = 4)
ggplot(data=envr, aes(x=inter_trade_cost,
                     y=value, fill=subnat, linetype=subnat)) +
  geom_col(position = "dodge", width = 0.15, linewidth = 2, color = 'darkgrey') +
  scale_fill_brewer(palette="Paired") +
  scale_x_reverse(breaks = c(max_tau, min_tau)) +
  geom_text(aes(label = subnat), vjust = -0.5, colour = "black", size = 3.5,
            position = position_dodge(width = 0.15)) +
  scale_y_continuous(limits=c(0.9,1.16), oob = rescale_none) + 
  scale_linetype_manual(values=c("longdash", "blank")) +
  theme_minimal() + labs(fill = NULL, x = "Trade Costs", y = NULL, linetype = NULL) + 
  theme(legend.position = "bottom",  text = element_text(size = 18),
    panel.spacing = unit(2, "lines"), legend.key.width= unit(2, 'cm')) +
  facet_wrap(~ variable)
ggsave("appendix_environment.pdf", width = 12, height = 4, units = "in")
dev.off()





normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, round(wages, digits = 3)]
reg[, wages_norm := round(wages, digits = 3)/ normalizer]

normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, round(labor, digits = 3)]
reg[, labor_norm := round(labor, digits = 3)/normalizer]

normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, round(wages/P_H, digits = 3)]
reg[, real_wage_norm := round(wages/P_H, digits = 3)/normalizer]

envr <- reg[, list(inter_trade_cost, subnat, labor_norm, real_wage_norm)] # wages_norm, 
envr <- melt(envr, 1:2)


envr[variable == "labor_norm", variable := "(a) Employment"]
# envr[variable == "wages_norm", variable := "(b) Nominal Wages"]
envr[variable == "real_wage_norm", variable := "(b) Real Wages"]

tikz(file = "appendix_labor.tex", width = 12, height = 4)
ggplot(data=envr, aes(x=inter_trade_cost,
                     y=value, fill=subnat, linetype=subnat)) +
  geom_col(position = "dodge", width = 0.15, linewidth = 2, color = 'darkgrey') +
  scale_fill_brewer(palette="Paired") +
  scale_x_reverse(breaks = c(max_tau, min_tau)) +
  geom_text(aes(label = subnat), vjust = -0.5, colour = "black", size = 3.5,
            position = position_dodge(width = 0.15)) +
  scale_y_continuous(limits=c(0.9,1.12), oob = rescale_none) + 
  scale_linetype_manual(values=c("longdash", "blank")) +
  theme_minimal() + labs(fill = NULL, x = "Trade Costs", y = NULL, linetype = NULL) + 
  theme(legend.position = "bottom",  text = element_text(size = 18),
      panel.spacing = unit(2, "lines"), legend.key.width= unit(2, 'cm')) +
  facet_wrap(~ variable)
ggsave("appendix_labor.pdf", width = 12, height = 4, units = "in")
dev.off()




normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, network_degree]
reg[, network_degree_norm := network_degree/normalizer]

normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, int_share]
reg[, int_share_norm := int_share/normalizer]

normalizer <- reg[subnat == "Core" & inter_trade_cost == max_tau, ttl_output]
reg[, ttl_output_norm := ttl_output/normalizer]

envr <- reg[, list(inter_trade_cost, subnat, network_degree_norm, int_share_norm, ttl_output_norm)]
envr <- melt(envr, 1:2)


envr[variable == "network_degree_norm", variable := "(a) Network Centrality"]
envr[variable == "int_share_norm", variable := "(b) Intermediate Input\nExpenditure Share"]
envr[variable == "ttl_output_norm", variable := "(c) Total Output"]

tikz(file = "appendix_network.tex", width = 12, height = 4)
ggplot(data=envr, aes(x=inter_trade_cost,
                     y=value, fill=subnat, linetype=subnat)) +
  geom_col(position = "dodge", width = 0.15, linewidth = 2, color = 'darkgrey') +
  scale_fill_brewer(palette="Paired") +
  scale_x_reverse(breaks = c(max_tau, min_tau)) +
  geom_text(aes(label = subnat), vjust = -0.5, colour = "black", size = 3.5,
            position = position_dodge(width = 0.15)) +
  scale_y_continuous(limits=c(0.8,1.4), oob = rescale_none) + 
  scale_linetype_manual(values=c("longdash", "blank")) +
  theme_minimal() + labs(fill = NULL, x = "Trade Costs", y = NULL, linetype = NULL) + 
  theme(legend.position = "bottom", 
      text = element_text(size = 18),
      panel.spacing = unit(2, "lines"), legend.key.width= unit(2, 'cm')) +
  facet_wrap(~ variable)
ggsave("appendix_network.pdf", width = 12, height = 4, units = "in")
dev.off()
